<?php


namespace EasySwoole\EasySwoole;


use App\Process\ImgDown;
use App\Process\ImgItem;
use App\Process\Page;
use EasySwoole\EasySwoole\AbstractInterface\Event;
use EasySwoole\EasySwoole\Swoole\EventRegister;
use EasySwoole\ORM\Db\Config;

class EasySwooleEvent implements Event
{
    public static function initialize()
    {
        date_default_timezone_set('Asia/Shanghai');

        ###### 注册 mysql orm 连接池 ######
        // $config = new \EasySwoole\ORM\Db\Config(Config::getInstance()->getConf('MYSQL'));
        // 【可选操作】我们已经在 dev.php 中进行了配置
        # $config->setMaxObjectNum(20); // 配置连接池最大数量
        // DbManager::getInstance()->addConnection(new Connection($config));

        ###### 注册 Redis 连接池 ######
        ## 正确
        //redis连接池注册(config默认为127.0.0.1,端口6379)
        // config是空配置,用户需手动配置.
        $instance    = \EasySwoole\EasySwoole\Config::getInstance();
        $config      = $instance->getConf('REDIS');
        $redisConfig = new \EasySwoole\Redis\Config\RedisConfig();
        $redisConfig->setHost($config['host']);
        $redisConfig->setAuth($config['auth']);
        $redisConfig->setDb("0");
        $redisConfig->setPort($config["port"]);
        $redisConfig->setTimeout($config["POOL_TIME_OUT"]);

        $redisPoolConfig = \EasySwoole\RedisPool\RedisPool::getInstance()->register($redisConfig, "redis");
        $redisPoolConfig->setMinObjectNum($config["POOL_MIN_NUM"]);
        $redisPoolConfig->setMaxObjectNum($config["POOL_MAX_NUM"]);
        ## 正确

        $mysqlConfig = $instance->getConf('MYSQL');
        // // 数据库连接配置
        $config = new Config();
        $config->setDatabase($mysqlConfig['database']);
        $config->setUser($mysqlConfig['user']);
        $config->setPassword($mysqlConfig['password']);
        $config->setHost($mysqlConfig['host']);
        // //连接池配置
        $config->setGetObjectTimeout(3.0); //设置获取连接池对象超时时间
        $config->setIntervalCheckTime(30 * 1000); //设置检测连接存活执行回收和创建的周期
        $config->setMaxIdleTime(15); //连接池对象最大闲置时间(秒)
        $config->setMaxObjectNum(200); //设置最大连接池存在连接对象数量
        $config->setMinObjectNum(5); //设置最小连接池存在连接对象数量
        $config->setAutoPing(5); //设置自动ping客户端链接的间隔

        $connection = new \EasySwoole\ORM\Db\Connection($config);
        \EasySwoole\ORM\DbManager::getInstance()->addConnection($connection);
        \EasySwoole\ORM\DbManager::getInstance()->addConnection($connection, 'write');

    }

    public static function mainServerCreate(EventRegister $register)
    {
        // 自定义进程
        $arg = [
            'listKey'         => 'page',
            'maxCoroutineNum' => 3,
            'clear'           => Page::CLEAR_NO
        ];

        $page = new Page('page', $arg, false, \EasySwoole\Component\Process\Config::PIPE_TYPE_SOCK_DGRAM, true);
        ServerManager::getInstance()->getSwooleServer()->addProcess($page->getProcess());


        $arg     = [
            'listKey'         => 'imgItem',
            'maxCoroutineNum' => 3,
            'clear'           => Page::CLEAR_NO
        ];
        $imgItem = new ImgItem('imgItem', $arg, false, \EasySwoole\Component\Process\Config::PIPE_TYPE_SOCK_DGRAM, true);
        ServerManager::getInstance()->getSwooleServer()->addProcess($imgItem->getProcess());

        $arg     = [
            'listKey'         => 'imgDown',
            'maxCoroutineNum' => 3,
            'clear'           => Page::CLEAR_NO
        ];
        $imgItem = new ImgDown('imgDown', $arg, false, \EasySwoole\Component\Process\Config::PIPE_TYPE_SOCK_DGRAM, true);
        ServerManager::getInstance()->getSwooleServer()->addProcess($imgItem->getProcess());
    }
}
